package com.eaoa.query.bus;

import java.util.Collection;
import java.util.Iterator;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;

import com.eaoa.base.EABaseQuery;
import com.eaoa.vo.bus.BusTransfer;

public class BusTransferQuery extends EABaseQuery{
	
	boolean lazyInitBusOrders= false;

	public BusTransferQuery() {
		super(BusTransfer.class);
		addInnerJoin(1, "busOrders", "busOrders");
	}
	
	public void setTransferno(Long val){
		setEQCondition("transferno", val);
	}
	
	public void setCreator(Long val){
		setEQCondition("creator", val);
	}
	
	public void setLazyInitBusOrders(boolean lazyInitBusOrders) {
		this.lazyInitBusOrders = lazyInitBusOrders;
	}
	
	protected void lazeInit() throws HibernateException {
		Iterator it = getResults().iterator();
		while (it.hasNext()) {
			BusTransfer vo = (BusTransfer) it.next();
			if (lazyInitBusOrders) {
				Hibernate.initialize(vo.getBusOrders());
			}
		}
	}
	
	//fetch order begin
	public void setOrderBillcode(String val){
		setLIKECondition("busOrders.billcode", val);
	}
	public void setOrderCustomerno(Long val){
		setEQCondition("busOrders.pubCustomer.customerno", val);
	}
	public void setOrderCustomerno(Collection val){
		setINCondition("busOrders.pubCustomer.customerno", val);
	}
	
	//fetch order end
	

	public void setBillcode(String val){
		setLIKECondition("billcode", val);
	}
	
	public void setBillstatus(String val){
		setEQCondition("billstatus", val);
	}
	
	public void setIslock(String val){
		setEQCondition("islock", val);
	}
	
	public void setFetchBusVehicle(){
		setLeftJoinFetchs(" left join fetch this.busVehicle busVehicle ");
	}
	
	public void setVehicleno(Long val){
		setEQCondition("busVehicle.vehicleno", val);
	}
	
	public static BusTransfer loadByNO(Long val){
		BusTransferQuery query = new BusTransferQuery();
		query.setFetchBusVehicle();
		query.setTransferno(val);
		query.execute();
		if(query.getResults().size()!=1){
			return null;
		}
		return (BusTransfer)query.getResults().get(0);
	}
	
	public static BusTransfer loadByNOInitPubOrders(Long val){
		BusTransferQuery query = new BusTransferQuery();
		query.setFetchBusVehicle();
		query.setTransferno(val);
		query.setLazy(true);
		query.setLazyInitBusOrders(true);
		query.execute();
		if(query.getResults().size()!=1){
			return null;
		}
		return (BusTransfer)query.getResults().get(0);
	}
}